package com.unicom.projt.reg;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.util.AlertDialogUtils;
import com.unicom.projt.util.CheckUtils;
import com.unicom.projt.util.PageJumpUtils;
import com.unicom.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午3:37
 */
public class RegisterStepOnePage extends Activity implements View.OnClickListener {
    private EditText mPassword;
    private EditText mRepeatPwd;
    private TextView mRegPhoneNumber;
    private RegisterController mController;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_stepone);
        mPassword=(EditText)this.findViewById(R.id.reg_password);
        mRepeatPwd=(EditText)this.findViewById(R.id.reg_rp_pwd);
        mRegPhoneNumber=(TextView)this.findViewById(R.id.reg_phone_number);
        mRegPhoneNumber.setText(UserHolder.phoneNumber);

        this.findViewById(R.id.reg_step_one_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.reg_step_one_back_btn).setOnClickListener(this);
        this.findViewById(R.id.verification_code_btn).setOnClickListener(this);

        mController=new RegisterController(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.reg_step_one_logout_btn:
                jumpToInputPhoneNumberPage();
                break;
            case R.id.reg_step_one_back_btn:
                jumpToInputPhoneNumberPage();
                break;

            case R.id.verification_code_btn:
                requestVerificationCode();
                break;
        }
    }

    private void jumpToInputPhoneNumberPage(){
        PageJumpUtils.jump(this, InputPhoneNumberPage.class);
        this.finish();
    }
    @Override
    public void onBackPressed() {
        jumpToInputPhoneNumberPage();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void requestVerificationCode() {
        String phoneNumber= UserHolder.phoneNumber;
        String password=mPassword.getText().toString().trim();
        String repeatPwd=mRepeatPwd.getText().toString().trim();
        if( "".equals(password) || "".equals(repeatPwd)){
            AlertDialogUtils.showConfirmDiaLog(this,"请完整填写.");
            return;
        }

        if(password.length()<6){
            AlertDialogUtils.showConfirmDiaLog(this,"密码最少要6位.");
            return;
        }
        if(!password.equals(repeatPwd)){
            AlertDialogUtils.showConfirmDiaLog(this,"两次密码不一致.");
            return;
        }
        showDialog(DIALOG_PROGRESS);
        mController.sendVerificationCode(phoneNumber,password);
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
